package com.wangcang.widgetlibrary.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.bumptech.glide.Glide;
import com.wangcang.widgetlibrary.R;
import com.wangcang.widgetlibrary.activity.base.BaseActivity;
import com.wangcang.widgetlibrary.bubble.PopUpUtils;
import com.wangcang.widgetlibrary.eventbus_event.UploadImageEvent;
import com.wangcang.widgetlibrary.media.UploadType;
import com.wangcang.widgetlibrary.photoview.PhotoView;
import com.wangcang.widgetlibrary.popupwindow.EasyPopupWindow;
import com.wangcang.widgetlibrary.tools.ThreadUtils;
import com.wangcang.widgetlibrary.viewindicator.CirclePageIndicator;
import com.wangcang.widgetlibrary.viewindicator.CustomViewPagerInternal;

import java.io.File;
import java.util.List;

import de.greenrobot.event.EventBus;

public class PreviewImageActivity extends BaseActivity implements EasyPopupWindow.ChildClickListener, View.OnClickListener {
    private CustomViewPagerInternal mCustomViewPagerInternal;
    private CirclePageIndicator mCirclePagerIndicator;
    private PagerAdapter mAdapter;

    private EasyPopupWindow mEasyPopupWindow;

    private List<UploadType> mSelectedUploadTypeList;
    private int mStartPosition;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_preview_image);

        initView();
        initData();
    }

    private void initView() {
        mCustomViewPagerInternal = findView(R.id.view_pager);
        mCirclePagerIndicator = findView(R.id.page_indicator);
    }

    private void initData() {
        if (getIntent().getExtras() != null) {
            mSelectedUploadTypeList = (List<UploadType>) getIntent().getSerializableExtra(UploadConfirmActivity.KEY_UPLOAD_LIST);
            mStartPosition = getIntent().getIntExtra(UploadConfirmActivity.KEY_UPLOAD_POSITION, 0);

            // +
            mSelectedUploadTypeList.remove(mSelectedUploadTypeList.size() - 1);
        }

        if (mSelectedUploadTypeList != null) {
            mCustomViewPagerInternal.setAdapter(mAdapter = new PagerAdapter() {
                @Override
                public int getCount() {
                    return mSelectedUploadTypeList.size();
                }

                @Override
                public View instantiateItem(ViewGroup container, int position) {
                    PhotoView photoView = new PhotoView(container.getContext());
                    UploadType uploadType = mSelectedUploadTypeList.get(position);
                    Glide.with(PreviewImageActivity.this)
                            .load(new File(uploadType.getPath()))
                            .placeholder(R.drawable.picture_no)
                            .into(photoView);
                    container.addView(photoView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

                    photoView.setOnLongClickListener(new View.OnLongClickListener() {
                        @Override
                        public boolean onLongClick(View v) {
                            mEasyPopupWindow = PopUpUtils.showPopUpBottom(R.layout.view_image_popupwindow_delete, PreviewImageActivity.this, findViewById(android.R.id.content), true, PreviewImageActivity.this);
                            return true;
                        }
                    });

                    return photoView;
                }

                @Override
                public void destroyItem(ViewGroup container, int position, Object object) {
                    container.removeView((View) object);
                }

                @Override
                public boolean isViewFromObject(View view, Object object) {
                    return view == object;
                }

                public int getItemPosition(Object object){
                    return POSITION_NONE;
                }
            });
        }

        mCirclePagerIndicator.setViewPager(mCustomViewPagerInternal, mStartPosition);
    }

    @Override
    public void getChildView(int layoutId, View view) {
        Button delete = (Button) view.findViewById(R.id.btn_delete_photo);
        Button cancel = (Button) view.findViewById(R.id.btn_cancel);

        delete.setOnClickListener(this);
        cancel.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_delete_photo:
                int currentPosition = mCustomViewPagerInternal.getCurrentItem();
                final File file = new File(mSelectedUploadTypeList.get(currentPosition).getPath());

                ThreadUtils.newThread(new Runnable() {
                    @Override
                    public void run() {
                        if (file != null) {
                            //删除系统图库文件
                            getContentResolver().delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, MediaStore.Images.Media.DATA + "=?", new String[] { file.getAbsolutePath() });

                            //通知图库更新
                            sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + file.getAbsolutePath())));

                            //删除本地文件
                            file.delete();
                        }
                    }
                });

                //更新ViewPager
                mSelectedUploadTypeList.remove(currentPosition);
                if (mSelectedUploadTypeList.size() > 0) {
                    mAdapter.notifyDataSetChanged();
                    mCirclePagerIndicator.notifyDataSetChanged();
                } else {
                    finishActivity();
                }

                //同步更新GridView
                EventBus.getDefault().post(new UploadImageEvent(currentPosition + ""));
                break;
            case R.id.btn_cancel:
                break;
            default:
                break;
        }

        if (mEasyPopupWindow != null && mEasyPopupWindow.isShowing()) {
            mEasyPopupWindow.dismiss();
        }
    }
}
